﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GetAllIPAdress
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Diaoyong();
        }

        public void Diaoyong()
        {
            try
            {
                string hostName = Dns.GetHostName();
                IPAddress[] ipAddress = Dns.GetHostAddresses(hostName);
                foreach (IPAddress address in ipAddress)
                {
                    if (address.AddressFamily != AddressFamily.InterNetwork)
                    {
                        continue;
                    }

                    cmbIPs.Items.Add(address.ToString());
                }

                if (cmbIPs.Items.Count > 0)
                {
                    cmbIPs.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            cmbIPs.Items.Clear();
            Diaoyong();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            txtResult.Clear();
            try
            {
              
                string[] ips = cmbIPs.Text.Split('.');
                string 网段 = "";
                for (int i = 0; i < 3; i++)
                {
                    网段 += ips[i] + ".";
                }

                Ping(网段);


            }
            catch (Exception exception)
            {
                txtResult.Text = exception.Message + Environment.NewLine;
            }

          
          
        }

        private void Ping(string 网段)
        {
            for (int i = 0; i < 255; i++)
            {
                try
                {
                    Ping myPing;
                    myPing = new Ping();
                    myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);

                    string pingIP = 网段 + i.ToString();
                    myPing.SendAsync(pingIP, 2000, null);
                }
                catch (Exception exception)
                {

                }

            }
        }

        private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            string rsl = e.Reply.Address.ToString();
            if (e.Reply.Status == IPStatus.Success)
            {
                //  rsl += " " + GetMacAddress(e.Reply.Address.ToString());
                this.Dispatcher.Invoke(() => { this.txtResult.AppendText($"{e.Reply.Address}\r\n"); });
                // this.listBox1.Items.Add(rsl);
            }
        }

      
    }
}
